package com.airlines.base.service.websocket.impl;

import java.util.concurrent.CopyOnWriteArraySet;

import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import com.airlines.base.config.websocket.BaseWebSocket;
import com.airlines.base.service.websocket.WebSocketService;

/**
 * @author xxxx
 * @date 2017年12月1日 
 * @description
 */
@Service
public class WebSocketServiceImpl implements WebSocketService{	
	@Override
	public void sendMessage(String webSocketType, String userName, String message)  {
		CopyOnWriteArraySet<BaseWebSocket> qmpWebSocketSet = BaseWebSocket.getQmpWebSocketSet();
		for(BaseWebSocket bws : qmpWebSocketSet){
			//判断哪些客户端需要发送消息
			boolean sendAbleFlag  = StringUtils.equals(bws.getWebSocketType(), webSocketType)
					&& StringUtils.equals(bws.getUserName(), userName);			
			//如果找到客户端，则发送消息
			if(sendAbleFlag){
				bws.sendMessage(message);
			}
		}
		
	}

	@Override
	public void sendMessage(String webSocketType, String message) {
		CopyOnWriteArraySet<BaseWebSocket> qmpWebSocketSet = BaseWebSocket.getQmpWebSocketSet();
		for(BaseWebSocket bws : qmpWebSocketSet){
			//判断哪些客户端需要发送消息
			boolean sendAbleFlag  = StringUtils.equals(bws.getWebSocketType(), webSocketType);			
			//如果找到客户端，则发送消息
			if(sendAbleFlag){
				bws.sendMessage(message);
			}
		}
	}

	@Override
	public int getOnlineCount(String webSocketType) {
		int onlineCount = 0;
		CopyOnWriteArraySet<BaseWebSocket> qmpWebSocketSet = BaseWebSocket.getQmpWebSocketSet();
		for(BaseWebSocket bws : qmpWebSocketSet){
			//判断客户端类型是否符合
			boolean sendAbleFlag  = StringUtils.equals(bws.getWebSocketType(), webSocketType);			
			if(sendAbleFlag){
				onlineCount++;
			}
		}
		return onlineCount;
	}

}
